var ns, map, app, timer, marqueesHeight, slide, imageBrowser,
    stopscroll, scrollElem, preTop, currentTop, stoptime, leftElem, popup;
app = new Vue({
    el: '#app',
    data: {
        form: {
            status: 1,//1-待接单 2-处理中 3-已完工 4-已关单
        },
        list: [], 
        isviewhistory: false,
        total: {
            WaitingCount: 0,
            ProcessCount: 0,
            CompleteCount: 0,
        },
        IsHistory: 0,//0 列表 1 历史工单
    },
    methods: {
        get_data: function() {
            var that = this;
            var options = {};
            options.status = that.form.status;
            options.IsHistory = that.IsHistory;
            options.action = "getqualitycontrollist";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.list = data.list;
                    that.total = data.countform;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_select_tab: function(status) {
            var that = this;
            that.form.status = status;
            that.get_data();
        },
        do_open: function(item) {
            var that = this;
            ns.openWin('qualitycontroldetail_frm', '工单详情', {
                id: item.ID,
                isviewhistory: that.isviewhistory
            })
        },
        //接单
        do_accpet: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getqualitystate";
            ns.post(options, function(succeed, data, err) {
                if(data.status == true && data.ServiceStatus == item.ServiceStatus){
                    that.do_canaccpet(item);
                }else{
                    api.toast({
                        msg: "状态已改变，请重新选择",
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });

                }
                
            })
        },
        do_canaccpet: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "acceptorderquality";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: "操作成功",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    else {
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.form.status = 2;
                    that.get_data();
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //整改完成
        do_zgcomplete:function(item){
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('qualitycontrolprocess_zhenggai', '整改完成', {
                        ID:item.ID,
                        ProjectID: item.ProjectID,
                        CheckUserID:item.CheckUserID,
                        CheckUserName:item.CheckUserName,
                        type:1,//1-整改完成，2-验收
                    });
                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                }
            });
            
        },
        //验收
        do_yanshou:function(item){
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('qualitycontrolprocess_yanshou', '整改验收', {
                        ID:item.ID,
                        ProjectID: item.ProjectID,
                        CheckUserID:item.CheckUserID,
                        CheckUserName:item.CheckUserName,
                        type:2,//1-整改完成，2-验收
                    });
                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                }
            });

        },

        //派单 重派
        do_open_chooseuser: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('chooseserviceuser_frm', '选择负责人', {
                        source: 'chooseuser',
                        id: item.ID,
                        type: 1,
                        ServiceType: 3
                    });

                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.get_data();
                }
            });
           
        },
        //完工
        do_complete: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('qualitycontrolprocess_frm', '录入完工记录', {
                        id: item.ID,
                        type: 2,
                    });
                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.get_data();
                }
            });      
        },
        //关单
        do_open_dialog: function (item) {
            var that = this;
            api.confirm({
                title: "提示",
                msg: '工单关闭后，将不能进行任何操作，请确认工单是否已经处理完成？',
                buttons: ['确认关闭', '取消']
            }, function (ret) {
                if (ret.buttonIndex == 1) {
                    var options = {};
                    options.ID = item.ID;
                    options.action = "closequality";
                    ns.post(options, function (succeed, data, err) {
                        if (succeed) {      
                            if (data.status) {
                                api.toast({
                                    msg: "操作成功",
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                            else {
                                api.toast({
                                    msg: data.Msg,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                            that.get_data();
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: true,
                        toastmsg: '提交中'
                    });
                }
            })
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.isviewhistory = api.pageParam.isviewhistory || false;
    app.get_data();
    api.addEventListener({
        name: 'reloadservicelist'
    }, function(ret, err) {
        app.get_data();
    });

    api.addEventListener({
        name: 'do_open_qualityhistory'
    }, function (ret, err) {
        ns.openWin('qualitycontrollist_history', '历史记录', {
            isviewhistory: true,
            // bg: "#289FFF",
            // txtcolor: "#ffffff",
        })
    });

};
